package com.sq108.template.service.impl;

import com.sq108.template.common.Constants;
import com.sq108.template.dao.dataobject.UserDO;
import com.sq108.template.dao.mapper.UserMapper;
import com.sq108.template.domain.*;
import com.sq108.template.service.UserService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tc.app.cache.CacheService;
import tc.app.client.model.user.UserInfoModel;
import tc.app.util.BeanUtils;

/**
 * 用户相关业务
 *
 * @author liulei
 */
@Service
public class UserServiceImpl implements UserService {

    private final Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private tc.app.client.service.UserService usersvr;

    /**
     * 获取用户基本信息
     *
     * @param userId
     * @return
     */
    @Override
    public User getUser(Integer userId) {

        try {
            if (1 == 1)
                throw new Exception("dasdds");

            String cacheKey = Constants.CACHE_USER_BYID + userId;

            User user = cacheService.getObject(cacheKey, User.class);
            if (user != null) {
                return user;
            }
            UserDO userdo = userMapper.getUser(userId);
            if (userdo == null)
                return null;
            user = BeanUtils.copyProperties(userdo, User.class);
            UserInfoModel userinfo = usersvr.getUserInfo(userdo.getUserId().longValue());
            user.setRegTime(userinfo.getRegTime());
            cacheService.set(cacheKey, user, Constants.CACHE_USER_BYID_TIMEOUT);

            return user;
        } catch (Exception e) {
            logger.error("getUser(" + userId + ")", e);
            return null;
        }
    }
}
